package Graph.AdMatrix;

import java.util.Scanner;

public class MGraph implements IGraph {
   public final static int INFINITY = Integer.MAX_VALUE;
   private GraphKind kind;
   private int vexNum,arcNum;
   private Object[] vexs1;
   private int[][] arcs1;
   public MGraph(GraphKind udn, int i, Object[] vex1, int[][] arcs1){
       this(null,0,0,null,null);
   }
   public MGraph(GraphKind kind,int vexNum,int arcNum,Object[] vexs1,int[][] arcs1){
       this.kind=kind;
       this.vexNum=vexNum;
       this.arcNum=arcNum;
       this.vexs1=vexs1;
       this.arcs1=arcs1;
   }

    @Override
    public void createGraph() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入图的类型：");
        GraphKind kind = GraphKind.valueOf(sc.next());
        switch (kind){
            case DG:
                createDG();
                return;
            case UDG:
                createUDG();
                return;
            case DN:
                createDN();
                return;
            case UDN:
                createUDN();
                return;
        }




    }

    private void createUDG() {

    }

    private void createDN() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请分别输入图的顶点数、图的边数：");
        vexNum = sc.nextInt();
        arcNum = sc.nextInt();
        vexs1 = new Object[vexNum];
        System.out.println("请分别输入图的各个顶点：");
        for(int v = 0; v<vexNum;v++)
            vexs1[v] = sc.next();
        arcs1=new int[vexNum][vexNum];
        for(int v=0;v<vexNum;v++)
            for(int u=0;u<vexNum;u++)
                arcs1[v][u]=INFINITY;
        System.out.println("请输入各个边的两个顶点及其权值：");
        for(int k =0;k<arcNum;k++){
            int v = locateVex(sc.next());
            int u = locateVex(sc.next());
            arcs1[v][u]=sc.nextInt();
        }

    }

    private void createUDN() {
       Scanner sc = new Scanner(System.in);
       System.out.println("请分别输入图的顶点数、图的边数：");
       vexNum = sc.nextInt();
       arcNum = sc.nextInt();
       vexs1 = new Object[vexNum];
       System.out.println("请分别输入图的各个顶点：");
       for(int v = 0; v<vexNum;v++)
           vexs1[v] = sc.next();
       arcs1=new int[vexNum][vexNum];
       for(int v=0;v<vexNum;v++)
           for(int u=0;u<vexNum;u++)
               arcs1[v][u]=INFINITY;
           System.out.println("请输入各个边的两个顶点及其权值：");
           for(int k =0;k<arcNum;k++){
               int v = locateVex(sc.next());
               int u = locateVex(sc.next());
               arcs1[v][u]=arcs1[u][v]=sc.nextInt();
           }


    }

    private void createDG() {


    }

    @Override
    public int getVexNum() {
        return vexNum;
    }

    @Override
    public int getArcNum() {
        return arcNum;
    }





    @Override
    public int locateVex(Object vex) {
        for(int v=0;v<vexNum;v++)
            if(vexs1[v].equals(vex))
                return v;
        return -1;
    }


    @Override
    public Object getVex(int v) throws Exception{
        if(v<0 && v>=vexNum)
            throw new Exception("第"+v+"个顶点不存在！");
        return vexs1[v];
    }

    @Override
    public int firstAdjVex(int v)throws Exception {

       if(v<0&&v>=vexNum)
           throw new Exception("第"+v+"个顶点不存在！");
       for (int j =0;j<vexNum;j++)
           if(arcs1[v][j]!=0&&arcs1[v][j]<INFINITY)
               return j;
           return -1;
    }

    @Override
    public int nextAdjVex(int v, int w)  throws Exception {
        if(v<0&&v>=vexNum)
            throw new Exception("第"+v+"个顶点不存在！");
        for (int j = w + 1; j < vexNum; j++)
            if(arcs1[v][j]!=0&&arcs1[v][j]<INFINITY)
                return j;
            return -1;

    }



    public GraphKind getKind(){
       return kind;
    }
    public int[][] getArcs(){
       return arcs1;
    }
    public Object[] getVexs(){
       return vexs1;
    }
}
